package com.yupi.usercenter.controller;


import com.yupi.usercenter.common.BaseResponse;
import com.yupi.usercenter.common.ErrorCode;
import com.yupi.usercenter.common.ResultUtils;
import com.yupi.usercenter.exception.BusinessException;
import com.yupi.usercenter.model.domain.User;
import com.yupi.usercenter.model.domain.request.UserLoginRequest;
import com.yupi.usercenter.model.domain.request.UserRegisterRequest;
import com.yupi.usercenter.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user")
public class userController {
    @Autowired
    private UserService userService;

    // 注册
    @PostMapping("/register")
    public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
        if (userRegisterRequest == null) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        long result = userService.userRegister(userRegisterRequest.getUserAccount(), userRegisterRequest.getUserPassword(), userRegisterRequest.getCheckPassword(), userRegisterRequest.getPlanetCode());
        return ResultUtils.success(result);
    }

    // 登录
    @PostMapping("/login")
    public BaseResponse<User> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        if (userLoginRequest == null) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        User user = userService.userLogin(userLoginRequest.getUserAccount(), userLoginRequest.getUserPassword(), request);
        return ResultUtils.success(user);
    }

    //查询用户，根据用户名查询
    @GetMapping("/search")
    public BaseResponse<List<User>> searchUserByName(@RequestParam("username") String username, HttpServletRequest request) {
        //只有管理员可以查询
        if (!isAdmin(request).getData()) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        List<User> userList = userService.searchUserByName(username);
        return ResultUtils.success(userList);
    }

    //删除用户，根据id删除
    @DeleteMapping("/delete")
    public BaseResponse<Boolean> deleteUser(@RequestParam("id") long id, HttpServletRequest request) {
        //只有管理员可以删除
        if (!isAdmin(request).getData()) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = userService.removeById(id);
        return ResultUtils.success(result);
    }

    //提取公共方法
    public BaseResponse<Boolean> isAdmin(HttpServletRequest request) {
        Object safeUser = request.getSession().getAttribute("userId");
        //先判断session
        if (safeUser == null) {
            return ResultUtils.error(ErrorCode.NOT_LOGIN, "未登录");
        }
        //走到这一步说明是登录过了，然后判断是否是管理员,根据userId查询
        User user = userService.getById((Serializable) safeUser);


        if (user.getRole() == 1) {
            return ResultUtils.success(true);
        }

        if (user.getRole() != 1) {
            return ResultUtils.error(ErrorCode.NO_AUTH, "无权限");
        }
        return ResultUtils.success(true);
    }

    //用户退出(也就是注销用户)
    @PostMapping("/logout")
    public BaseResponse<Integer> userLogout(HttpServletRequest request) {
        if (request == null) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        int result = userService.userLogout(request);
        return ResultUtils.success(result);
    }
}
